home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2017 October / PCgo 10-2017 CD-ROM Germany.iso / nw.pak / Unnamed File 004873.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  4.0 KB  |  132 lines

  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. define("mojo/public/js/connector", [
  6.   "mojo/public/js/buffer",
  7.   "mojo/public/js/codec",
  8.   "mojo/public/js/core",
  9.   "mojo/public/js/support",
  10. ], function(buffer, codec, core, support) {
  11.  
  12.   function Connector(handle) {
  13.     if (!core.isHandle(handle))
  14.       throw new Error("Connector: not a handle " + handle);
  15.     this.handle_ = handle;
  16.     this.dropWrites_ = false;
  17.     this.error_ = false;
  18.     this.incomingReceiver_ = null;
  19.     this.readWaitCookie_ = null;
  20.     this.errorHandler_ = null;
  21.  
  22.     this.waitToReadMore_();
  23.   }
  24.  
  25.   Connector.prototype.close = function() {
  26.     if (this.readWaitCookie_) {
  27.       support.cancelWait(this.readWaitCookie_);
  28.       this.readWaitCookie_ = null;
  29.     }
  30.     if (this.handle_ != null) {
  31.       core.close(this.handle_);
  32.       this.handle_ = null;
  33.     }
  34.   };
  35.  
  36.   Connector.prototype.accept = function(message) {
  37.     if (this.error_)
  38.       return false;
  39.  
  40.     if (this.dropWrites_)
  41.       return true;
  42.  
  43.     var result = core.writeMessage(this.handle_,
  44.                                    new Uint8Array(message.buffer.arrayBuffer),
  45.                                    message.handles,
  46.                                    core.WRITE_MESSAGE_FLAG_NONE);
  47.     switch (result) {
  48.       case core.RESULT_OK:
  49.         // The handles were successfully transferred, so we don't own them
  50.         // anymore.
  51.         message.handles = [];
  52.         break;
  53.       case core.RESULT_FAILED_PRECONDITION:
  54.         // There's no point in continuing to write to this pipe since the other
  55.         // end is gone. Avoid writing any future messages. Hide write failures
  56.         // from the caller since we'd like them to continue consuming any
  57.         // backlog of incoming messages before regarding the message pipe as
  58.         // closed.
  59.         this.dropWrites_ = true;
  60.         break;
  61.       default:
  62.         // This particular write was rejected, presumably because of bad input.
  63.         // The pipe is not necessarily in a bad state.
  64.         return false;
  65.     }
  66.     return true;
  67.   };
  68.  
  69.   Connector.prototype.setIncomingReceiver = function(receiver) {
  70.     this.incomingReceiver_ = receiver;
  71.   };
  72.  
  73.   Connector.prototype.setErrorHandler = function(handler) {
  74.     this.errorHandler_ = handler;
  75.   };
  76.  
  77.   Connector.prototype.encounteredError = function() {
  78.     return this.error_;
  79.   };
  80.  
  81.   Connector.prototype.waitToReadMore_ = function() {
  82.     this.readWaitCookie_ = support.asyncWait(this.handle_,
  83.                                              core.HANDLE_SIGNAL_READABLE,
  84.                                              this.readMore_.bind(this));
  85.   };
  86.  
  87.   Connector.prototype.readMore_ = function(result) {
  88.     for (;;) {
  89.       var read = core.readMessage(this.handle_,
  90.                                   core.READ_MESSAGE_FLAG_NONE);
  91.       if (this.handle_ == null) // The connector has been closed.
  92.         return;
  93.       if (read.result == core.RESULT_SHOULD_WAIT) {
  94.         this.waitToReadMore_();
  95.         return;
  96.       }
  97.       if (read.result != core.RESULT_OK) {
  98.         this.error_ = true;
  99.         if (this.errorHandler_)
  100.           this.errorHandler_.onError(read.result);
  101.         return;
  102.       }
  103.       var messageBuffer = new buffer.Buffer(read.buffer);
  104.       var message = new codec.Message(messageBuffer, read.handles);
  105.       if (this.incomingReceiver_) {
  106.           this.incomingReceiver_.accept(message);
  107.       }
  108.     }
  109.   };
  110.  
  111.   // The TestConnector subclass is only intended to be used in unit tests. It
  112.   // enables delivering a message to the pipe's handle without an async wait.
  113.  
  114.   function TestConnector(handle) {
  115.     Connector.call(this, handle);
  116.   }
  117.  
  118.   TestConnector.prototype = Object.create(Connector.prototype);
  119.  
  120.   TestConnector.prototype.waitToReadMore_ = function() {
  121.   };
  122.  
  123.   TestConnector.prototype.deliverMessage = function() {
  124.     this.readMore_(core.RESULT_OK);
  125.   }
  126.  
  127.   var exports = {};
  128.   exports.Connector = Connector;
  129.   exports.TestConnector = TestConnector;
  130.   return exports;
  131. });
  132.